﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccess.Model.Production;

namespace DataAccess.Modules.DataManager.Production
{
    public class CathegoryDataManager : DataAccess.BaseObjects.DataManager
    {

        public void Insert(BusinessObject.Production.Cathegory cathegory)
        {

            MyGeneration.dOOdads.TransactionMgr transaction = MyGeneration.dOOdads.TransactionMgr.ThreadTransactionMgr();
            var modelCathegoryDescription = new ProductionCathegoryDescription();
            var modelCathegoryHerency = new ProductionCathegoryHerency();

            try
            {

                transaction.BeginTransaction();

                modelCathegoryDescription.ConnectionString = this.ConnectionString;
                modelCathegoryDescription.AddNew();
                modelCathegoryDescription.Name = cathegory.Name;
                modelCathegoryDescription.Description = cathegory.Description;
                modelCathegoryDescription.Save();

                modelCathegoryHerency.ConnectionString = this.ConnectionString;
                modelCathegoryHerency.AddNew();
                modelCathegoryHerency.IdCathegory = modelCathegoryDescription.IdCathegory;
                modelCathegoryHerency.IdParentCathegory = cathegory.ParentId;
                modelCathegoryHerency.Save();

                transaction.CommitTransaction();

            }
            catch (Exception)
            {

                transaction.RollbackTransaction();
                MyGeneration.dOOdads.TransactionMgr.ThreadTransactionMgrReset();
                throw;

            }

        }


        /*MATI: la logica que ves en este procedimiento es por la estructura del registro que se modifica y que se puede modificar distintos
            atributos. Ahora la logica de negocios por ejemplo ... se tiene que encargar de que aca no llegue una categoria sin un ID cargado
            que es el necesario para hacer las consultas y con ningun atributo cargado porque no tiene sentido*/

        public void Update(BusinessObject.Production.Cathegory cathegory)
        {
            MyGeneration.dOOdads.TransactionMgr transaction = MyGeneration.dOOdads.TransactionMgr.ThreadTransactionMgr();
            var modelCathegoryDescription = new ProductionCathegoryDescription();
            var modelCathegoryHerency = new ProductionCathegoryHerency();

            try
            {

                transaction.BeginTransaction();

                if (!String.IsNullOrEmpty(cathegory.Name) || !String.IsNullOrEmpty(cathegory.Description))
                {
                    modelCathegoryDescription.ConnectionString = this.ConnectionString;

                    if (modelCathegoryDescription.LoadByPrimaryKey(cathegory.Id))
                    {
                        if (!String.IsNullOrEmpty(cathegory.Name))
                        {
                            modelCathegoryDescription.Name = cathegory.Name;
                        }
                        if (!String.IsNullOrEmpty(cathegory.Description))
                        {
                            modelCathegoryDescription.Description = cathegory.Description;
                        }
                        modelCathegoryDescription.Save();
                    }
                }

                if (cathegory.ChangeHerency)
                {
                    modelCathegoryHerency.ConnectionString = this.ConnectionString;
                    modelCathegoryHerency.Where.IdCathegory.Value = cathegory.Id;

                    if (modelCathegoryHerency.Query.Load())
                    {
                        modelCathegoryHerency.IdParentCathegory = cathegory.ParentId;
                    }
                    modelCathegoryHerency.Save();
                }

                transaction.CommitTransaction();

            }
            catch (Exception)
            {

                transaction.RollbackTransaction();
                MyGeneration.dOOdads.TransactionMgr.ThreadTransactionMgrReset();
                throw;

            }

        }

        public void DeleteByKey(BusinessObject.Production.Cathegory cathegory)
        {
            MyGeneration.dOOdads.TransactionMgr transaction = MyGeneration.dOOdads.TransactionMgr.ThreadTransactionMgr();
            var modelCathegoryDescription = new ProductionCathegoryDescription();
            var modelCathegoryHerency = new ProductionCathegoryHerency();
            var modelCathegoryOwnProduct = new ProductionCathegoryOwnProduct();

            try
            {
                //Definir diseño del borrado logico en la base de datos
                transaction.BeginTransaction();

                modelCathegoryHerency.ConnectionString = this.ConnectionString;
                modelCathegoryHerency.Where.IdCathegory.Value = cathegory.Id;

                if (modelCathegoryHerency.Query.Load())
                {
                    modelCathegoryHerency.DeleteAll();
                }

                modelCathegoryOwnProduct.ConnectionString = this.ConnectionString;
                modelCathegoryOwnProduct.Where.IdCathegory.Value = cathegory.Id;

                if (modelCathegoryOwnProduct.Query.Load())
                {
                    modelCathegoryOwnProduct.DeleteAll();
                }

                modelCathegoryDescription.ConnectionString = this.ConnectionString;

                if (modelCathegoryDescription.LoadByPrimaryKey(cathegory.Id))
                {
                    modelCathegoryDescription.DeleteAll();
                }

                transaction.CommitTransaction();

            }
            catch (Exception)
            {

                transaction.RollbackTransaction();
                MyGeneration.dOOdads.TransactionMgr.ThreadTransactionMgrReset();
                throw;

            }
        }

        public void DeleteByHerency(BusinessObject.Production.Cathegory cathegory)
        {

        }
    }
}
